package DataBase;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Vector;

public class AddCommentAboutOverHrs {
	
	
	
	private Vector<String> workHours;
    private int FlagHrs;
	public AddCommentAboutOverHrs(Vector<String> empHours, int FlagHrs)
	{
		this.workHours=empHours;
		this.FlagHrs=FlagHrs; 
	}
	
	
	public void saveComment()
	{
		
		if(FlagHrs==1)                                 // ne bil sovershen vihod v techenii 12 chasov
		{
		SQLConnection sc=new SQLConnection();
		try {
			  
			PreparedStatement ps1 = sc.getConnection().prepareStatement("SELECT max(work_hrs_id) FROM employee_working_hours WHERE Eid=?");   //("SELECT work_hrs_id FROM employee_working_hours WHERE Eid=? and date=? and clock_out=null");        //("SELECT MAX(work_hrs_id) MAXid FROM employee_working_hours WHERE Eid=? and date=?;");
				//	sc.getConnection().setAutoCommit(false); // ubrat
					ps1.setString(1,(String) workHours.get(0));
				 
					ResultSet rsps=ps1.executeQuery(); 
					int work_hrs_idInt=0;                   
		   
					while (rsps.next())
					{
						
						work_hrs_idInt=rsps.getInt(1);

						if(work_hrs_idInt==0){
						 
							work_hrs_idInt=0;
						}
									
				
					} 
					
			PreparedStatement ps2 = sc.getConnection().prepareStatement("UPDATE employee_working_hours set comments=? WHERE Eid=? and work_hrs_id=?");       // and clock_in=? and clock_out=?");      //("INSERT INTO employee_working_hours set comments=? WHERE Eid=? and date=? and clock_out=?;");  
			  
			sc.getConnection().setAutoCommit(false);
			ps2.setString(1,"exit warning");	 
			
			ps2.setString(2,(String) workHours.get(0));
			ps2.setInt(3,work_hrs_idInt);  
			ps2.executeUpdate();
			sc.getConnection().commit();
			ps2.close();
			 
			sc.getConnection().close();
		 
							
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			
			e.printStackTrace();
			
		}
		
			
		}
		else if(FlagHrs!=1)  
		{
			SQLConnection sc=new SQLConnection();
			try {
				 
				PreparedStatement ps2 = sc.getConnection().prepareStatement("UPDATE employee_working_hours set comments=? WHERE Eid=? and date=? and clock_out=?");       // and clock_in=? and clock_out=?");      //("INSERT INTO employee_working_hours set comments=? WHERE Eid=? and date=? and clock_out=?;");  
				  
				sc.getConnection().setAutoCommit(false);
				ps2.setString(1,"check hours");	 
				 
				ps2.setString(2,(String) workHours.get(0));
				ps2.setString(3,(String) workHours.get(1));                                 
				ps2.setString(4,(String) workHours.get(2));     
			
				ps2.executeUpdate();
				sc.getConnection().commit();
				ps2.close();
				 
				sc.getConnection().close();
			 
								
			}
			catch (SQLException e) {
				// TODO Auto-generated catch block
				
				e.printStackTrace();
				
			}
			
				
			}
			
	 
}
	
}
